package com.mydemo.game.hireStage;

import com.mydemo.entity.Car;
import com.mydemo.entity.Save;

import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LeaseCars {
    public static final boolean leaseCars(Save save, Car car) {
        System.out.println(car.getCarBrand() + car.getCarModel() + car.getCarModelDetail() + "的日租金是：" + car.getRentPrise());
        System.out.println("请输入需要租赁的天数，输入0退出");
        Scanner scanner = new Scanner(System.in);
        int days = scanner.nextInt();
        if (days == 0) {
            return false;
        } else if (days * car.getRentPrise() > save.getMoney()) {
            System.out.println("你的资金不足，以自动取消租赁");
        } else {
            save.setMoney(save.getMoney() - days * car.getRentPrise());
            car.setDays(days);
            List<Car> cars;
            if (save.getCarList() == null) {
                car.setId(1);
                cars = new ArrayList<>();
            } else {
                cars = save.getCarList();
                int id = cars.stream()
                        .mapToInt(Car::getId)
                        .max()
                        .orElse(0) + 1; // 找到当前列表中id的最大值并加1
                car.setId(id);
            }
            cars.add(car);
            save.setCarList(cars);
            return true;
        }
        return false;
    }
}
